<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>


<p>
    <logic:present name="listCarOrderDetail">
    <h1> Your code order: ${carOrderDTO.orderId}</h1>
    <h1>Order date: ${carOrderDTO.orderDate}</h1>
    <form name="form1" >
        <table width="100%" border="0" style="border-bottom-width:0;border-left-width:0;border-right-width:0">
            <tr>
                <td>Carorderdetail id</td>
                <td>Car name</td>
                <td>Quanlity</td>
                <td>
                    Driver
                </td>
                <td>Pickup date</td>
                <td>Dropoff date</td>
                <td>Total</td>
                <td align="right" colspan="2">
                    <input type="checkbox" id="checkall" value="check for cancel" onclick="checkAll(document.form1.check,document.form1.detailId,document.form1.total,document.form1.pickup);">
                </td>
            </tr>

            <logic:iterate id="CarOrderDetail" name="listCarOrderDetail">
                <tr onmousemove="style.backgroundColor='#E9EFF8'" onmouseout="style.backgroundColor='#ffffff'">
                    <td><input type="text" size="2" readonly name="detailId" value="<bean:write name="CarOrderDetail" property="carOderDetailId"/>"></td>
                    <td><bean:write name="CarOrderDetail" property="carsDTO.carName"/></td>
                    <td><bean:write name="CarOrderDetail" property="qty"/></td>
                    <td>
                        <logic:equal name="CarOrderDetail" property="driver"  value="0">
                            No
                        </logic:equal>
                        <logic:equal name="CarOrderDetail" property="driver"  value="1">
                            Yes
                        </logic:equal>

                    </td>
                    <td><input type="text" readonly size="10" value="<bean:write name="CarOrderDetail" property="pickUpdate"/>" name="pickup"></td>
                    <td><bean:write name="CarOrderDetail" property="dropOffDate"/></td>
                    <td>
                        <input type="text" size="8" readonly value="<bean:write name="CarOrderDetail" property="total"/>" name="total">
                    </td>

                    <td align="right" colspan="2">
                        <input type="checkbox" value="check" name="check" id="check<bean:write name="CarOrderDetail" property="carOderDetailId"/>" onclick="checkt(document.form1.check,document.form1.detailId,document.form1.total,document.form1.pickup)">
                    </td>
                </tr>

            </logic:iterate>
          
                <tr style="visibility:hidden">
                <td><input type="text" size="2" readonly name="detailId" value="0"></td>
                <td></td>
                <td></td>
                <td></td>
                <td><input type="text" readonly size="10" value="9/9/2010" name="pickup"></td>
                <td></td>
                <td>
                    <input type="text" size="8" readonly value="0" name="total">
                </td>

                <td align="right" colspan="2">
                    <input type="checkbox" value="check" name="check" id="check<bean:write name="CarOrderDetail" property="carOderDetailId"/>" onclick="checkt(document.form1.check,document.form1.detailId,document.form1.total,document.form1.pickup)">
                </td>
            </tr>
        </table>
    </form>
    <br/><br/>
    The refund rules are given as below:<br/>
    <br/><font color="red">
        Before 1 day -- 75%<br/>
        2 days -- 80%<br/>
        3 days -- 85%<br/>
        4 days -- 90%<br/>
        5 days or more -- 95%
    </font>
    <h1> The amount you paid: ${money}</h1>
    <div id="result" >
   
    </div>
</logic:present>
<logic:notPresent name="listCarOrderDetail" >
    ${mess}
</logic:notPresent>
</p>

<script>
    function checkAll(field,detailId,total,pickup)
    {
        
        if(document.form1.checkall.checked == true)
        {
            value = "";
            j = 0;
            tongtienthoi = 0;
            for (i = 0; i < field.length - 1; i++)
            {
                field[i].checked = true ;
                j += 1;
                value +=  "<tr>" +
                    "<td>"+"<input type='text' size='2' readonly name='"+"detailId"+j+"'" +"id='"+"detailId"+j+"'" + "value='"+detailId[i].value +"'"+">"+"</td>"+
                    "<td>"+pickup[i].value +"</td>"+
                    "<td>"+total[i].value +"</td>"+
                    "<td>"+ ngayhuy(pickup[i].value) +"</td>"+
                    "<td>"+"<input type='text' size='2' readonly name='"+"money"+j+"'" +"id='"+"money"+j+"'" + "value='"+tienthoi(ngayhuy(pickup[i].value),total[i].value) +"'"+">"+
                    "</td>"
                    +"</tr>";
                tongtienthoi += tienthoi(ngayhuy(pickup[i].value),total[i].value);
            }
            document.getElementById('result').innerHTML =
                "<form action='CarOrder.do'>"+
                "<input type='hidden'  size='2' name='num' value='"+j +"'"+">"
                + "<table border='0' width='100%'><tr bgcolor='#C2D1D8'><td>DetailId</td><td>Pickup date</td><td>Total</td><td>Before</td><td>Cash Blance</td>"
                + value +
                "</tr></table>" + "<br/> Cash Blance" +tongtienthoi +"<br/>"+"<input type='submit' name='action' value='CancelOrder'>"
                +"</form>";
            return true;

        }else{
            for (i = 0; i < field.length; i++)
            {
                field[i].checked = false ;
                document.getElementById('result').innerHTML  ="Information you choose not to cancel";
            }
            return false;
        }
       

    }

    function checkt(field,detailId,total,pickup){
        flag = true;
        value = "";
        j = 0;
        tongtienthoi = 0;
        
        for (i = 0; i < (field.length - 1); i++)
        {
            if(field[i].checked == true){
                j += 1;
                value +=  "<tr>" +
                    "<td>"+"<input type='text' size='2' readonly name='"+"detailId"+j+"'" +"id='"+"detailId"+j+"'" + "value='"+detailId[i].value +"'"+">"+"</td>"+
                    "<td>"+pickup[i].value +"</td>"+
                    "<td>"+total[i].value +"</td>"+
                    "<td>"+ ngayhuy(pickup[i].value) +"</td>"+
                    "<td>"+"<input type='text' size='2' readonly name='"+"money"+j+"'" +"id='"+"money"+j+"'" + "value='"+tienthoi(ngayhuy(pickup[i].value),total[i].value) +"'"+">"+
                    "</td>"
                    +"</tr>";
                tongtienthoi += tienthoi(ngayhuy(pickup[i].value),total[i].value);
            }else{
                flag = false;
            }

        }
        if(flag == false){
            document.form1.checkall.checked = false;
        }else{
            document.form1.checkall.checked = true;
        }
        if(value == ""){
            document.getElementById('result').innerHTML ="Information you choose not to cancel";
        }else{
            document.getElementById('result').innerHTML =
                "<form action='CarOrder.do'>"+
                "<input type='hidden'  size='2' name='num' value='"+j +"'"+">"
                + "<table border='0' width='100%'><tr bgcolor='#C2D1D8'><td>DetailId</td><td>Pickup date</td><td>Total</td><td>Before</td><td>Cash Blance</td>"
                + value +
                "</tr></table>" + "<br/> Cash Blance" +tongtienthoi +"<br/>"+"<input type='submit' name='action' value='CancelOrder'>"
                +"</form>";
        }
    }
   
    function ngayhuy(elementdate){
        datetime = new  Date();
        month = datetime.getMonth()+ 1;
        day = datetime.getDate();
        year = datetime.getFullYear();
        date = month + "/"+day + "/"+year;
        tien = 0;
        Date1 = new Date (date);
        Date2 = new Date (elementdate);
        DaysDiff = Math.floor ((Date2 - Date1))/86400000;
        return DaysDiff;
    }
    function tienthoi(DaysDiff,total){
        switch(DaysDiff){
            case 1:{
                    tien = total * 75 / 100;
                    break;
                }
            case 2:{
                    tien = total * 80 / 100;
                    break;
                }
            case 3:{
                    tien = total * 85 / 100;
                    break;
                }
            case 4:{
                    tien = total * 90 / 100;
                    break;
                }
            default:{
                    tien = total * 95 / 100;
                    break;
                }
        }
        return tien;
    }
</script>
